package com.bicon.botu.finance.wallet.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.finance.wallet.service.WalletDetailService;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.WalletDetail;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.finance.wallet.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: sx
 * @Date: Created in 16:52 2017/6/21/021
 */
@RestController
@RequestMapping("/V1.0.0/api/wallet")
public class WalletApi extends BaseController {

    @Autowired
    private WalletService walletService;

    @Autowired
    private WalletDetailService walletDetailService;

    @Autowired
    private RedisHandler redisHandler;

    /**
     * 查找账户余额
     *
     * @return
     */
    @RequestMapping(value = "/balance", method = RequestMethod.POST)
    public BaseResult balance() {
        return walletService.selectBalance(getUserId());
    }

    /**
     * 查找是否设置支付密码
     *
     * @return
     */
    @RequestMapping(value = "/selectPaypwd", method = RequestMethod.POST)
    public BaseResult selectPaypwd() {
        return walletService.selectPaypwd(getUserId());
    }

    /**
     * 根据老密码修改密码
     *
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @RequestMapping(value = "/changePwd", method = RequestMethod.POST)
    public BaseResult changePwd(@RequestParam(value = "oldPwd") String oldPwd,
                                @RequestParam(value = "newPwd") String newPwd) {
        return walletService.updatePaypasswordByPwd(getUserId(), oldPwd, newPwd);
    }

    /**
     * 校验原密码
     *
     * @param oldPwd
     * @return
     */
    @RequestMapping(value = "/checkOldPwd", method = RequestMethod.POST)
    public BaseResult checkOldPwd(@RequestParam(value = "oldPwd") String oldPwd) {
        return walletService.checkOldPwd(getUserId(), oldPwd);
    }

//    /**
//     * 设置支付密码
//     *
//     * @param newPwd
//     * @param code
//     * @param phone
//     * @return
//     */
//    @RequestMapping(value = "/addPwd", method = RequestMethod.POST)
//    public BaseResult addPwd(@RequestParam(value = "newPwd") String newPwd,
//                             @RequestParam(value = "code") String code,
//                             @RequestParam(value = "phone") String phone) {
//        return walletService.setPaypasswordByCode(getUserId(), newPwd, code, phone);
//    }

    /**
     * 设置支付密码
     *
     * @param newPwd
     * @return
     */
    @RequestMapping(value = "/addPwd", method = RequestMethod.POST)
    public BaseResult addPwd(@RequestParam(value = "newPwd") String newPwd) {
        return walletService.setPaypassword(getUserId(), newPwd);
    }

    /**
     * 查找交易明细
     *
     * @return
     */
    @RequestMapping(value = "/selectDetail", method = RequestMethod.POST)
    public BaseResult selectDetail(@RequestParam(value = "pageSize") int pageSize,
                                   @RequestParam(value = "pageNo") int pageNo,
                                   @RequestParam(value = "subjects", required = false) String subjects) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", getUserId());
        if (StringUtils.isNotBlank(subjects)) {
            map.put("subjects", subjects);
        }
        pageNo = pageNo <= 0 ? 0 : pageNo;
        pageSize = pageSize <= 0 ? 10 : pageSize;
        Page<WalletDetail> page = new Page<>(pageNo, pageSize);
        return walletDetailService.selecetDetailByParam(page, map);
    }

    /**
     * 充值
     *
     * @param type  充值类型
     * @param money 充值金额
     * @param code  应用code
     * @return
     */
    @RequestMapping(value = "/recahrge", method = RequestMethod.POST)
    public BaseResult recahrge(@RequestParam String type, @RequestParam String money, @RequestParam String code,@RequestParam String appId,
                              @RequestParam String module,@RequestParam String business) {
        return walletService.recahrge( appId,  module,  business,getUserId(), type, money, code);
    }

    /**
     * 提现
     *
     * @param financialId
     * @param money
     * @return
     */
//    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
//    public BaseResult withdraw(String financialId, double money, String password) {
//        if (money < 30d || money > 200d) {
//            return new BaseResult(WalletConstants.RESPONSE_CODE_401, WalletConstants.RESPONSE_MESSAGE_401);
//        }
//        return walletService.subtractBalance(getUserId(), money, password, financialId);
//    }

    /**
     * 取消支付
     *
     * @param orderNo  订单号
     * @return
     */
    @RequestMapping(value = "/cancelPay", method = RequestMethod.POST)
    public BaseResult cancelPay(@RequestParam String orderNo) {
        return walletService.cancelPay(getUserId(), orderNo);
    }

    /**
     * 转账
     *
     * @param money  金额
     * @param toUserId   接收人
     * @param payPwd 支付密码
     * @param remark 备注
     * @param payType 钱包0、支付宝1、微信2
     * @param code 传APP的标识 BOTU
     * @return
     */
    @RequestMapping(value = "/transfer",method = RequestMethod.POST)
    public BaseResult transfer(@RequestParam(value = "money") double money,
                               @RequestParam(value = "toUserId") String toUserId,
                               @RequestParam(value = "payPwd") String payPwd,
                               @RequestParam(value = "remark") String remark,
                               @RequestParam(value = "payType") String payType,
                               @RequestParam(value = "code")  String code,
                               @RequestParam(value = "appId")  String appId,@RequestParam(value = "module")  String module,@RequestParam(value = "business")  String business) {
        return walletService.transfer( appId,  module,  business, money,getUserId(), toUserId, payPwd, remark, payType, code);
    }

    /**
     * 转账确认接口
     * @param orderNo 订单号
     * @return
     */
    @RequestMapping(value = "/transferConfirm",method = RequestMethod.POST)
    public BaseResult transferConfirm(@RequestParam(value = "orderNo") String orderNo) {
        return walletService.transferConfirm(getUserId(), orderNo);
    }

    /**
     * 转账详情接口
     * @param orderNo 订单号
     * @param flag 标志 0转账方，1收钱方
     * @return
     */
    @RequestMapping(value = "/transferInfo",method = RequestMethod.POST)
    public BaseResult transferInfo(@RequestParam(value = "orderNo") String orderNo, @RequestParam(value = "flag") String flag) {
        if("0".equals(flag)){
            return sendResult200(new WalletDetail().selectOne(new EntityWrapper().eq("user_id", getUserId()).eq("order_no", orderNo)));
        }
        if("1".equals(flag)){
            return sendResult200(new WalletDetail().selectOne(new EntityWrapper().eq("other_user_id", getUserId()).eq("order_no", orderNo)));
        }
        return sendResult400();
    }

    /**
     * 支付成功确认接口
     * @return
     */
    @RequestMapping(value = "/paymentAppNotify",method = RequestMethod.POST)
    public BaseResult paymentAppNotify(String orderNo, String payType) throws IOException {
        String result  = walletService.payConfirm(getUserId(), orderNo, payType, "");
        if("success".equals(result)){
            return sendResult200();
        }
        return sendResult400();
    }


    /**
     * 支付成功确认接口 异步 主动
     * @return
     */
    @RequestMapping(value = "/paymentAppNotifyAsy",method = RequestMethod.POST)
    public BaseResult paymentAppNotifyAsy(@RequestParam(value = "orderNo") String orderNo,@RequestParam(value = "userId") String userId) throws IOException {
        WalletDetail walletDetail  = walletDetailService.selectOne(new EntityWrapper<WalletDetail>().eq("pay_order_no",orderNo).eq("user_id",userId));
        if(null==walletDetail){
          return sendResult500();
        }
        if(WalletConstants.WALLET_STATE_SUCCESS.equals(walletDetail.getState())){
            return sendResult200();
        }
        return sendResult400();
    }



}
